package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    //http://localhost:8080/setmeal
    @PutMapping("")
    public ResultInfo update(@RequestBody Setmeal setmeal){
        setmealService.updateSetmeal(setmeal);
       return ResultInfo.success(null);
    }
    //http://localhost:8080/setmeal/1524284875622223874
    @GetMapping("/{id}")
    public ResultInfo findById(@PathVariable("id") Long id){
        Setmeal setmeal=setmealService.findById(id);
        return ResultInfo.success(setmeal);
    }
    //http://localhost:8080/setmeal/status/0?ids=1524284875622223874
    @PostMapping("status/{status}")
    public ResultInfo updateStatus(@PathVariable("status") Integer status,Long[] ids){
        if (ids!=null&&ids.length>0){
            setmealService.updateStatus(status,ids);
        }
        return ResultInfo.success(null);
    }
    //http://localhost:8080/setmeal?ids=1524285624519397377
    @DeleteMapping("")
    public ResultInfo delete(Long[] ids) {
        if (ids!=null&&ids.length>0){
            setmealService.deleteByIds(ids);
        }
        return ResultInfo.success(null);
    }
    //http://localhost:8080/setmeal
    @PostMapping("")
    public ResultInfo save(@RequestBody Setmeal setmeal){
        setmealService.add(setmeal);
        return ResultInfo.success(null);
    }

    //http://localhost:8080/setmeal/page?page=1&pageSize=10
    @GetMapping("/page")
    public ResultInfo selectPage(@RequestParam("page") Integer pageNo, Integer pageSize, String name){
        Page<Setmeal> page = new Page<>(pageNo, pageSize);
        page=setmealService.selectPage(page,name);
        return ResultInfo.success(page);
    }
}
